<template>
  <div>
    <el-dialog title="附件管理" :visible.sync="dialogVisible" width="60%">
      <el-row style="margin-top:1rem;">
        <el-col>
          <el-upload style="float:right;margin-right:2rem;" :show-file-list="false" :on-success="handleSuccess"
            :action="url">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-col>
      </el-row>
      <el-row style="margin-top:2rem;margin-bottom:2rem;">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="id" prop="id">
          </el-table-column>
          <el-table-column label="附件名" prop="name">
          </el-table-column>
          <el-table-column label="创建时间" width="180" prop="createTime">
          </el-table-column>
          <el-table-column label="更新时间" width="180" prop="updateTime">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="danger" @click="deleteOne(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
  export default {
    data: function () {
      return {
        dialogVisible: false,
        url: window.SITE_CONFIG['baseUrl'] + '/attachment/upload?&token=' + this.$cookie.get('token'),
        tableData: []
      }
    },
    methods: {
      init() {
        this.dialogVisible = true
        this.getData()
      },
      getData() {
        this.$http({
          url: `${this.$http.adornUrl('/attachment/list?token='+this.$cookie.get('token') )}`,
          method: 'get',
        }).then(data => {
          this.tableData = data.data.list
        })
      },
      deleteOne(row) {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: `${this.$http.adornUrl('/attachment/'+row.id+'?token='+this.$cookie.get('token') )}`,
            method: 'delete',
          }).then(({
            data
          }) => {
            if (data['code'] == 0) {
              this.$message({
                message: '删除成功',
                type: 'success'
              });
              this.getData()
            } else {
              this.$message({
                message: '数据删除错误',
                type: 'error'
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleSuccess(res) {
        console.log(res)
        if (res.code == 0) {
          this.$message({
            message: '上传成功',
            type: 'success'
          });
          this.getData()
        }
      }

    }

  }

</script>
